package com.example.springmvcdemo.comtroller;

import com.example.springmvcdemo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
@Slf4j
@RequestMapping("/test")
@ResponseBody
@Controller
public class TestController {
    @RequestMapping(value="/hi")
    public String sayA(String name,int v){
        return "amagi"+name+"  v="+v;
    }
    @GetMapping("/class")
    public String showUser(User user){
        return user.toString();
    }

    @GetMapping("/show-time")
    public String showTime(@RequestParam("t") String StartTime, @RequestParam("t2") String EndTime){
        return "开始时间"+StartTime+"  |结束时间"+EndTime;
    }
    @PostMapping("/show-json")
    public String showJson(@RequestBody User user){
        return user.toString();
    }

    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username") String name,@PathVariable("password") String password){
        return name+":"+password;
    }

    @RequestMapping("/upfile")
    public String upfile(@RequestPart("mylife")MultipartFile file) throws IOException {
        String path="/d:";
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/myupfile")
    public String myupfile(@RequestPart("myfile") MultipartFile file){
        //根目录
        String path="D/:";
        //根目录加唯一的文件名
        path+= UUID.randomUUID().toString();
        //根目录+唯一文件名+文件后缀
        path+=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
        return path;
    }
    @GetMapping("/getparam")
    public String getParam(HttpServletRequest req){
        return req.getParameter("username");
    }
    @RequestMapping("/getck")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            log.error(cookie.getName()+":"+cookie.getValue());
        }
        return "getcookie";
    }
    //获取单个的Cookie
    @RequestMapping("/getck2")
    public String getCookie2(@CookieValue("zhangsan") String val){
        return "Cookie Value:"+val;
    }
    @RequestMapping("/getua")
    public String getUA(@RequestHeader("User-Agent")String userAgent){
        return userAgent;
    }
    @RequestMapping("/setsess")
    public String setSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("userinfo","userinfo");
        return "Set Session Success.";
    }

    @RequestMapping("/getsess")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession(false); //一定要加false
        if(session!=null && session.getAttribute("userinfo")!=null){
            return (String)session.getAttribute("userinfo");
        }else{
            return "暂无Session信息";
        }
    }

    //使用注解获取Session的方式
    @RequestMapping("/getsess2")
    public String getSession2(@SessionAttribute(value="userinfo",required = false)String userinfo){
        return userinfo;
    }
    @RequestMapping("/respjson")
    public HashMap<String,String> respJson(){
        HashMap<String,String> map=new HashMap<>();
        map.put("Java","Java Value");
        return map;
    }

    @RequestMapping("/hello")
    public String getHello(){
        return "hello world";
    }
}
